package yxy.game.pm2.command.conquest;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ConquestMessage;
import yxy.game.pm2.module.bandit.BanditHandle;
import yxy.game.pm2.module.conquest.ConquestHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

/** 征战 征战数据更新 */
@MessageType(proto = Command.ConquestDataUpdate, classtype = ConquestMessage.ConquestDataUpdateRequest.class)
public class CMD_ConquestDataUpdate extends Command<ConquestMessage.ConquestDataUpdateRequest> {

	@Override
	public void execute(MessageSource messageSource, ConquestMessage.ConquestDataUpdateRequest request) {
		String playerCode = messageSource.getTag();

		ConquestMessage.ConquestDataUpdateResponse.Builder response = ConquestMessage.ConquestDataUpdateResponse.newBuilder();

		ConquestModule conquestModule = ConquestModule.get(playerCode);
		int mapId = conquestModule.getMapId();
		long time = new ConquestHandle().get挂机时长(conquestModule);

		response.setMapId(mapId);
		response.setTime(time);

		send(messageSource, response, Result.Success);

		new BanditHandle().updateBanditTip(playerCode);// 剿匪刷新
	}

	private void send(MessageSource messageSource, ConquestMessage.ConquestDataUpdateResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}